# Google command line interface
# Copyright (C) 2005 by Deryck Hodge
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


import re, sys

tags = re.compile('<(.)*?>')
html_chars = {
    '&quot;' : '"',
    '&amp;' : '&',
    '&lt;' : '<',
    '&gt;' : '>',
}


def clean_html(text):
    if tags.search(text):
        text = tags.sub('', text)

    for char in html_chars.keys():
        if char in text:
            text = text.replace(char, html_chars[char])

    return text


class Display:

    def __init__(self, format, results, search=None):
        self.format = format
        self.results = results
        self.search = search
        self.opts = ['url', 'title', 'excerpt', 'brief', 'all', 'google']

        if self.format not in self.opts:
            print "You must specify a valid display format.\n"
            sys.exit(2)

        if self.format == 'url':
            for result in self.results:
                for hit in result:
                    print hit.URL

        if self.format == 'title':
            for result in self.results:
                for hit in result:
                    if hit.title != '':
                        print clean_html(hit.title)
                    else:
                        print '<Untitled>'

        if self.format == 'excerpt':
            for result in self.results:
                for hit in result:
                    if hit.title != '':
                        print clean_html(hit.title)
                    else:
                        print '<Untitled>'
                    print hit.URL
                    print clean_html(hit.snippet) + '\n'

        if self.format == 'brief':
            for result in self.results:
                for hit in result:
                    if hit.title != '':
                        print clean_html(hit.title)
                    else:
                        print '<Untitled>'
                    print hit.URL + '\n'


        if self.format == 'all':
            for result in self.results:
                for hit in result:
                    if hit.title != '':
                        print clean_html(hit.title)
                    else:
                        print '<Untitled>'
                    print hit.URL
                    if hit.summary != '':
                        print 'Summary: ' + hit.summary
                    print hit.cachedSize + ' in Google cache'
                    print clean_html(hit.snippet) + '\n'

        if self.format == 'google':
            print \
""" 

/~_ _  _  _ | _   Web  Images  Groups  News  Froogle  Local  Scholar   more >>
\_/(_)(_)(_||(/_ |_________________________________________________| | Search |
          _|   

_______________________________________________________________________________
Web                         Results 1 - """ + str(self.search.max_results) \
+ ' of about ' + str(self.search.count()) + ' for ' + self.search.query + '\n' 

            for result in self.results:
                for hit in result:
                    if hit.title != '':
                        print clean_html(hit.title)
                    else:
                        print '<Untitled>'
                    print '--------------------------------------------------'
                    print clean_html(hit.snippet)
                    print '[[ ' + hit.URL + ' - ' + hit.cachedSize + ' - _Cached_ ]]\n' 
